<?php
// +----------------------------------------------------------------------
// | UCToo [ Universal Convergence Technology ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2020 https://www.uctoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Patrick <contact@uctoo.com>
// +----------------------------------------------------------------------
namespace app\admin\model\wechatopen;

use app\admin\model\wechatopen\Applet;
use think\Model;


class AdminApplet extends Model
{
    // 表名
    protected $name = 'admin_wechat_applet';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public static function selectApplet($admin_id, $wechatapplet_id)
    {
        self::where('admin_id','=',$admin_id)->delete();  //删除当前管理员选中的applet
        $res = self::create(['admin_id'=>$admin_id,'wechatapplet_id'=>$wechatapplet_id]);

        return $res;
    }

    public static function getApplet($admin_id)
    {
        $adminApplet = self::where('admin_id','=',$admin_id)->find();  //返回当前管理员选中的applet
        $applet = Applet::get(['wechatapplet_id'=>$adminApplet['wechatapplet_id']]);
        return $applet;
    }

    public static function getAppletConfig($admin_id)
    {
        $adminApplet = self::where('admin_id','=',$admin_id)->find();  //返回当前管理员选中的applet配置
        $applet = Applet::get(['wechatapplet_id'=>$adminApplet['wechatapplet_id']]);
        $adminAppletConfig = ['app_id'=>$applet['appid'],
            'secret' =>$applet['appsecret'],
            'token'=> $applet['token'],
            'aes_key' =>$applet['aeskey'],
            'wechatapplet_id' =>$applet['wechatapplet_id']];

        return $adminAppletConfig;
    }

    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function wechatopenapplet()
    {
        return $this->belongsTo('app\admin\model\wechatopen\Applet', 'wechatapplet_id', 'wechatapplet_id', [], 'LEFT')->setEagerlyType(0);
    }
}
